package rhinestone.Control;

import rhinestone.Model.Lease;
import rhinestone.Model.LeaseContainer;
import rhinestone.Model.Machinery;
import rhinestone.Model.MachineryContainer;
import rhinestone.Model.Staff;
import rhinestone.Model.Customer;

/**
 * Controls all the leasing concerned things.
 *
 * @author Group 2
 * @version 2010.12.12
 */
public class LeasingController {
	private LeaseContainer leaseContainer;
	private MachineryContainer machineryContainer;
	private Lease lease;
	private CustomerController customerController;

	/**
	 * Constructor, which does nothing.
	 */
	public LeasingController(LeaseContainer leaseContainer, MachineryContainer machineryContainer) {
		this.leaseContainer = leaseContainer;
		this.machineryContainer = machineryContainer;
	}

	public void initializeLease(Staff staff, Customer customer) {
		lease = new Lease(staff, customer);
	}

	/**
	 * Called by UI when the employee scans machinery bar code and wants to add it the current session.
	 * @param barcode The bar code of the scanned machinery.
	 */
	public void addMachinery(String barcode, int period) {
		Machinery machinery = machineryContainer.getMachinery(barcode);
		if(machinery != null)
			lease.addMachinery(machinery, period);
		else
			System.err.println("Machinery with bar code " + barcode + " does NOT exists.");
	}

	/**
	 * Called by UI when the employee finishes scanning the machinery bar codes,
	 * and wants to check the lease out.
	 * @param customerCpr The cpr of the customer. If provided - checks for available discounts. If 'null', does nothing with it.
	 */
	public void checkoutLease() {
		if(lease.getMachineryCount() > 0) {
			lease.changeMachineryStatus();
			leaseContainer.addLease(lease);
			System.out.println(" --- Lease checked out ---");
		}
	}

	/**
	 * Called by UI when the employee choose 'remove machinery' options,
	 * to remove the particular machinery from the current lease.
	 * Used if the machinery is added by mistake or something.
	 * @param barcode The bar code of the machinery to remove.
	 */
	public void removeMachinery(String barcode) {
		Machinery machinery = machineryContainer.getMachinery(barcode);
		lease.removeMachinery(machinery);
	}

	public Lease getLease(int id) {
		return leaseContainer.getLease(id);
	}

	/**
	 * Called by UI when the employee chooses to 'cancel lease'.
	 * It will destroy the current session together with all scanned machinery.
	 */
	public void cancelLease() {
		lease = null;
		System.out.println(" --- Lease canceled ---");
	}

	public Lease getCurrentLease() {
		return lease;
	}
}
